package com.autumn.zero.listing;

import java.util.List;
import java.util.function.Consumer;

import com.autumn.mybatis.criterion.EntityQuery;
import com.autumn.mybatis.criterion.Query;
import com.autumn.mybatis.mapper.IEntityMapper;

/**
 * 实体表达式
 * 
 * @author 杨昌国 2018-12-13 18:10:09
 * @param <TEntity> 实体类型
 * @param <TEntityMapper> 映射类型
 */
public class AutumnEntityZeroListingExpression<TEntity, TEntityMapper extends IEntityMapper<TEntity>>
		implements IAutumnZeroListingExpression {

	private final Class<TEntity> entityClass;
	private final TEntityMapper entityMapper;
	private final EntityQuery<TEntity> entityQuery;

	/**
	 * 
	 * @param entityClass
	 * @param entityMapper
	 * @param consumer
	 */
	public AutumnEntityZeroListingExpression(Class<TEntity> entityClass, TEntityMapper entityMapper,
			Consumer<EntityQuery<TEntity>> consumer) {
		this.entityClass = entityClass;
		this.entityMapper = entityMapper;
		this.entityQuery = new EntityQuery<>(entityClass);
		if (consumer != null) {
			consumer.accept(entityQuery);
		}
	}

	@Override
	public Query getQuery() {
		return this.entityQuery;
	}

	@Override
	public Class<?> getResultClass() {
		return this.entityClass;
	}

	@Override
	public int executeCount(Query query) {
		return this.entityMapper.countByWhere(query.builderSection());
	}

	@Override
	public List<TEntity> executeResult(Query query) {
		List<TEntity> entitys = this.entityMapper.selectList(query.builderSection());
		return entitys;
	}
}
